<?php


namespace app\index\controller;

use app\index\model\Banner;
use app\index\model\Crane;
use app\index\model\Plan as PlanModel;
use app\index\model\News as NewsModel;
use app\index\model\GoodsCat;
use app\index\model\ProductionStage;
use app\index\model\Record;
use app\index\model\SegmentType;
use think\admin\Controller;

//统计
class Chart extends Base
{
   

    //统计列表
    public function index()
    {
        $car_id =  request()->get('car_id/d',0);
        $start_time =   request()->get('start_time','');
        $end_time =   request()->get('end_time','');
        $where  = [];
        if($car_id > 0) $where[] = ['crane_id','=',$car_id];
        $this->carne_list       = Crane::select();
        $query  = PlanModel::where($where)->with(['crane','segment','stage'])->where($where);
        if($start_time !=''){
            $query->whereTime('start_time','>=',$start_time);
        }
        if($end_time !=''){
            $query->whereTime('end_time','<=',$end_time);
        }
        $this->list = $query->order('id desc')->append(['status_text'])->select();
        $this->car_id = $car_id;
        $this->start_time = $start_time;
        $this->end_time   = $end_time;
        return $this->fetch();
    }   


    //日志时间线
    public function times(){
        $craneId = input('get.crane_id', '');
        $timeRange = input('get.time_range', '');
        $startDate = input('get.start_date', '');
        $endDate = input('get.end_date', '');
        $query = Record::with(['process','plan'])->where('status',1)->order('id desc');
        if (!empty($craneId)) {
            $query->where('crane_id', $craneId);
        }
        if (!empty($timeRange)) {
            $start = date('Y-m-d H:i:s', strtotime("-$timeRange days"));
            $query->whereBetweenTime('start_time', $start, date('Y-m-d H:i:s'));
        }
        if (!empty($startDate) && !empty($endDate)) {
            $query->whereBetweenTime('start_time', $startDate, $endDate);
        }
        $this->record_list = $query->append(['circle'])->select();
        // $this->record_list = Record::with(['process','plan'])->where('status',1)->order('id desc')->select();
        $this->cranes = Crane::field('id,name')->select();
        return $this->fetch();
    }

}
